using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class TuiDAL
    {
        public static int Save(TuiInfo info)
        {
            string sql = string.Format("insert into [tui]([AdminID],[UserID],[OrderID],[OrderDetailID],[ProductID],[Type],[Reason],[Money],[Mobile],[Context],[CreateDate]) values(@AdminID,@UserID,@OrderID,@OrderDetailID,@ProductID,@Type,@Reason,@Money,@Mobile,@Context,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@OrderID", info.OrderID),
                new SqlParameter("@OrderDetailID", info.OrderDetailID),
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@Type", info.Type),
                new SqlParameter("@Reason", info.Reason),
                new SqlParameter("@Money", info.Money),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@Context", info.Context),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(TuiInfo info)
        {
            string sql = string.Format("update [tui] set [AdminID]=@AdminID,[OrderID]=@OrderID,[OrderDetailID]=@OrderDetailID,[ProductID]=@ProductID,[Type]=@Type,[Reason]=@Reason,[Money]=@Money,[Mobile]=@Mobile,[Context]=@Context where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@OrderID", info.OrderID),
                new SqlParameter("@OrderDetailID", info.OrderDetailID),
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@Type", info.Type),
                new SqlParameter("@Reason", info.Reason),
                new SqlParameter("@Money", info.Money),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@Context", info.Context),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static TuiInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[tui]");
            return SimpleData(dr);
        }
        public static List<TuiInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[tui]");
            return ListSimpleData(dt);
        }
        public static List<TuiInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [tui].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [tui].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[tui]", out count);
            return ListSimpleData(dt);
        }
        public static List<TuiInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [tui]"));
            return ListSimpleData(dt);
        }
        private static TuiInfo SimpleData(DataRow dr)
        {
            TuiInfo info = new TuiInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.OrderID = DataConvert.ToInt(dr["OrderID"]);
                info.OrderDetailID = DataConvert.ToInt(dr["OrderDetailID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.Type = DataConvert.ToString(dr["Type"]);
                info.Reason = DataConvert.ToString(dr["Reason"]);
                info.Money = DataConvert.ToFloat(dr["Money"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static TuiInfo AllData(DataRow dr)
        {
            TuiInfo info = new TuiInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.OrderID = DataConvert.ToInt(dr["OrderID"]);
                info.OrderDetailID = DataConvert.ToInt(dr["OrderDetailID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.Type = DataConvert.ToString(dr["Type"]);
                info.Reason = DataConvert.ToString(dr["Reason"]);
                info.Money = DataConvert.ToFloat(dr["Money"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static List<TuiInfo> ListSimpleData(DataTable dt)
        {
            List<TuiInfo> list = new List<TuiInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<TuiInfo> ListAllData(DataTable dt)
        {
            List<TuiInfo> list = new List<TuiInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

